<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Logistik extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper('bantuan_helper');
		$this->load->model('Logistik_model');
		if(!$this->session->userdata('user_id') && !$this->session->userdata('user_display_name') ):
			redirect('backend');
		elseif($this->session->userdata('user_rule')=='Sales'):
			redirect('backend/no_acces');
		endif;
	}

	function index()
	{
		$this->load->library('pagination');
		$offset = $this->uri->segment(4);
		$config['base_url'] = site_url().'/backend/logistik/index';
		$config['total_rows'] = count($this->Logistik_model->getLogistik('all',FALSE,FALSE,FALSE,FALSE,FALSE));
		$config['per_page'] = $this->config->item('per_page');
		$config['uri_segment'] = '4';
		
		$data['urut'] = $this->uri->segment(4);
		$data['title'] = 'Logistik';
		$data['template'] = 'logistik/index';
		$data['link']= '';
		$data['res'] = $this->Logistik_model->getLogistik('all',FALSE,$config['per_page'],$offset,FALSE,FALSE);
		$this->pagination->initialize($config);
		$this->load->view('backend/index',$data);
	}

	function add()
	{
		$this->form_validation->set_rules('log_nama', 'Nama','required');
		$this->form_validation->set_rules('log_biaya', 'Biaya','required');
		$this->form_validation->set_rules('prov_id', 'Province','required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['title'] = 'Logistik';
			$data['template'] = 'logistik/add';
			$this->load->view('backend/index',$data);
		}
		else
		{
			$data = array
				(
				'log_nama'=>$this->input->post('log_nama'),
				'log_biaya'=>$this->input->post('log_biaya'),
				'log_prov_id'=>$this->input->post('prov_id'),
				'log_kab_id'=>$this->input->post('kab_id'),
				);
			$this->Logistik_model->addLogistik($data);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Satu data berhasil ditambahkan</div>');
			redirect('backend/logistik');
		}	
	}

	function edit()
	{

		$this->form_validation->set_rules('log_nama', 'Nama','required');
		$this->form_validation->set_rules('log_biaya', 'Biaya','required');
		$this->form_validation->set_rules('prov_id', 'Province','required');

		if ($this->form_validation->run() == FALSE)
		{
			$id = $this->uri->segment(4);
			$data['title'] = 'Logistik';
			$data['template'] = 'logistik/edit';
			$data['row'] = $this->Logistik_model->getLogistik(FALSE,$id,FALSE,FALSE,FALSE,FALSE);
			$this->load->view('backend/index',$data);
		}
		else
		{
			$id = $this->uri->segment(4);
			$data = array
				(
				'log_nama'=>$this->input->post('log_nama'),
				'log_biaya'=>$this->input->post('log_biaya'),
				'log_prov_id'=>$this->input->post('prov_id'),
				'log_kab_id'=>$this->input->post('kab_id'),
				);
			$this->Logistik_model->editLogistik($id,$data);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data Berhasil diupdate</div>');
			redirect('backend/logistik/edit/'.$id);
		}
			
	}

	function delete()
	{
		$id = $this->uri->segment(4);
		$uri = $this->uri->segment(5);
		$this->Logistik_model->deleteLogistik($id);
		$count = count($this->Logistik_model->getLogistik('all',FALSE,FALSE,FALSE,FALSE,FALSE));
		$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil dihapus</div>');
		getPage('logistik','delete',$uri,$count);

	}
	
	function search_result()
	{
		$log_nama = $this->input->post('log_nama');
		$prov_id = $this->input->post('prov_id');
		
		$this->load->library('pagination');
		$offset = $this->uri->segment(4);
		$config['base_url'] = site_url().'/backend/logistik/search_result';
		$config['total_rows'] = count($this->Logistik_model->getLogistik('where',FALSE,FALSE,FALSE,$log_nama,$prov_id));
		$config['per_page'] = $this->config->item('per_page');
		$config['uri_segment'] = '4';
		
		$data['urut'] = $this->uri->segment(4);
		$data['title'] = 'Hasil Cari Logistik';
		$data['template'] = 'logistik/index';
		$data['link']= '<a href ='.site_url().'backend/logistik.php><< Back</a>';
		$data['res'] = $this->Logistik_model->getLogistik('where',FALSE,$config['per_page'],$offset,$log_nama,$prov_id);
		$this->pagination->initialize($config);
		$this->load->view('backend/index',$data);

	}
	
	function ajax_bulk_action()
	{
		foreach($_POST["logistik_id"] as $id):
			$this->db->where('logistik_id',$id);
			$this->db->delete('tb_logistik');
		endforeach;
	}

}